<template>
  <div>
    <van-nav-bar
      title="添加地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="content">
      <van-address-edit
        :address-info="editingContact"
        :area-list="area"
        show-postal
        show-delete
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
      />
    </div>
  </div>
</template>

<script>
import area from '@utils/area'
export default {
  name: 'contactedit',
  data () {
    return {
      area
    }
  },
  methods: {
    onClickLeft () {
      this.$router.replace('/contactlist')
    },
    onSave (item) {
      const id = this.$store.state.Address.data.findIndex(
        (el) => el.id === parseInt(this.$route.params.id)
      )
      this.$store.commit('Address/update_address', { id, item })
      this.$store.commit('Order/change_contact', item)
      this.$toast('更新成功')
      this.$router.replace('/contactlist')
    },
    onDelete () {
      const id = this.$store.state.Address.data.findIndex(
        (el) => el.id === parseInt(this.$route.params.id)
      )
      this.$store.commit('Address/del_address', id)
      this.$router.replace('/contactlist')
    }
  },
  computed: {
    editingContact () {
      return this.$store.state.Address.data[
        this.$store.state.Address.data.findIndex(
          (el) => el.id === parseInt(this.$route.params.id)
        )
      ]
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
